# coding=utf-8

import os
import xml.etree.ElementTree as ET

import sqlite3Utils


def getReg(filename) -> list:
    tree = ET.ElementTree(file=filename)
    root = tree.getroot()
    bigarray = []
    for child_of_root in root:
        note = child_of_root.attrib['note']
        for child_of_child in child_of_root:
            smallarray = []
            smallarray.append(child_of_child.text)
            smallarray.append(note)
            for item in child_of_child.attrib.values():
                smallarray.append(item)
            bigarray.append(smallarray)
    return bigarray


def createtbl(dbname, tblname):
    db = sqlite3Utils.MySqlite(dbname)
    # 创建表
    if not os.path.exists(os.path.join(os.getcwd(), dbname)):
        sql = "CREATE TABLE IF NOT EXISTS {}(id integer NOT NULL PRIMARY KEY AUTOINCREMENT, content TEXT, reason TEXT, type TEXT, note TEXT);".format(
            tblname)
        db.execute(sql)
        # print("{}表已创建".format(tblname))
    else:
        sql = "CREATE TABLE IF NOT EXISTS {}(id integer NOT NULL PRIMARY KEY AUTOINCREMENT, content TEXT, reason TEXT, type TEXT, note TEXT);".format(
            tblname)
        db.execute(sql)
        # print("{}表已创建".format(tblname))


def inittbl(dbname, tblname, data):
    db = sqlite3Utils.MySqlite(dbname)
    # 插入数据
    flag = db.add(tblname, data)
    return flag
